<template>
    <div class="product-item">
        <div class="product-image">
            <img :src="product.image" :alt="product.name">
            <div v-if="product.stock <= 0" class="sold-out">已售罄</div>
        </div>
        <div class="product-info">
            <h3 class="product-name">{{ product.name }}</h3>
            <div class="price-section">
                <span class="original-price">¥{{ product.price }}</span>
                <span class="seckill-price">¥{{ product.seckillPrice }}</span>
            </div>
            <div class="progress-bar">
                <div class="progress" :style="{ width: progress + '%' }"></div>
                <span class="progress-text">已抢{{ progress }}%</span>
            </div>
            <button
                    class="buy-btn"
                    :class="{ disabled: product.stock <= 0 }"
                    @click="$emit('buy', product.id)"
            >
                {{ product.stock <= 0 ? '已售罄' : '立即抢购' }}
            </button>
        </div>
    </div>
</template>

<script setup>
    import { computed } from 'vue';

    const props = defineProps({
        product: {
            type: Object,
            required: true
        }
    });

    const emit = defineEmits(['buy']);

    const progress = computed(() => {
        const total = props.product.stock + props.product.sold;
        return total > 0 ? Math.round((props.product.sold / total) * 100) : 0;
    });
</script>

<style scoped>
    .product-item {
        background: #fff;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        transition: transform 0.3s ease;
    }

    .product-item:hover {
        transform: translateY(-5px);
        box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
    }

    .product-image {
        position: relative;
        height: 180px;
        overflow: hidden;
    }

    .product-image img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: transform 0.5s ease;
    }

    .product-item:hover .product-image img {
        transform: scale(1.05);
    }

    .sold-out {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.6);
        color: white;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 20px;
        font-weight: bold;
    }

    .product-info {
        padding: 15px;
    }

    .product-name {
        margin: 0 0 10px;
        font-size: 16px;
        height: 40px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    .price-section {
        margin: 10px 0;
    }

    .original-price {
        color: #999;
        text-decoration: line-through;
        font-size: 14px;
        margin-right: 10px;
    }

    .seckill-price {
        color: #ff4e4e;
        font-size: 18px;
        font-weight: bold;
    }

    .progress-bar {
        height: 20px;
        background: #b5aaaa;
        border-radius: 10px;
        margin: 15px 0;
        position: relative;
        overflow: hidden;
    }

    .progress {
        height: 100%;
        background: linear-gradient(to right, #ff8a8a, #ff4e4e);
        border-radius: 10px;
        transition: width 0.5s ease;
    }

    .progress-text {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 12px;
        color: white;
    }

    .buy-btn {
        width: 100%;
        padding: 10px;
        background: linear-gradient(to right, #ff4e4e, #ff8a8a);
        color: white;
        border: none;
        border-radius: 4px;
        font-size: 16px;
        font-weight: bold;
        cursor: pointer;
        transition: all 0.3s ease;
    }

    .buy-btn:hover {
        background: linear-gradient(to right, #ff3a3a, #ff6b6b);
        box-shadow: 0 4px 12px rgba(255, 78, 78, 0.3);
    }

    .buy-btn.disabled {
        background: #ccc;
        cursor: not-allowed;
        box-shadow: none;
    }
</style>